package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.xmy.cultivate.entity.Quarter;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 每年季度 Mapper 接口
 * </p>
 *
 * @author min
 * @since 2022-08-22
 */
public interface QuarterMapper extends BaseMapper<Quarter> {


    @Select("select year_part from `quarter` where deleted = 0 GROUP BY year_part")
    public abstract List<Map<String,Object>> getYearPartList();

    @Select("select id,name,num,start_date,end_date,two_course_date,year_part from `quarter` ${ew.customSqlSegment}")
    public abstract IPage<Map<String,Object>> findAll(IPage<?> page, @Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select max(year_part) as yearPart from `quarter` where deleted = 0")
    public Integer getMaxYearPart();

    @Select("select * from `quarter` where deleted = 0 and year_part= #{yearPart} and num = #{num}")
    public Quarter getNow(@Param("yearPart") String yearPart,@Param("num") Integer num);

    /**
     * 获取结束id最大的一条数据
     * @param endId
     * @return
     */
    @Select("select * from `quarter` where id <=${endId} ORDER BY id desc limit 1 ")
    public Quarter getQuarterMaxForId(@Param("endId") Integer endId);

    @Select("select IFNULL(MAX(id),0) as id from `quarter` where year_part = ${yearPart} and num = ${num} limit 1 ")
    public Integer getIdForYearPartAndNum(@Param("yearPart") Integer yearPart,@Param("num") Integer num);

}
